package com.wanmait.deliverymangementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.mapper.OrdersMapper;
import com.wanmait.deliverymangementsystem.pojo.HistoryOrders;
import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.pojo.Tasks;
import com.wanmait.deliverymangementsystem.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrdersService {
@Resource
    private OrdersMapper ordersMapper;
@Resource
private TaskService taskService;

@Resource
private HistoOrderService histoOrderService;

@Resource
private UsersService usersService;
@Resource
private RunnerService runnerService;
@Resource
private UserAddressService userAddressService;

    @Override
    public void create(Orders orders) {
        ordersMapper.insert(orders);
    }



    @Override
    public PageInfo<Orders> adminFindAll(Integer page) {
            if (page==null)
                page=0;
        PageHelper.startPage(page,7);
        List<Orders> ordersList=ordersMapper.findAll();
        for (Orders orders : ordersList) {
            orders.setTask(taskService.findByID(orders.getTask().getId()));
            orders.setUsers(usersService.findUser(orders.getUsers().getId()));
            if (orders.getRunner()!=null)
            orders.setRunner(runnerService.findById(orders.getRunner().getId()));
        }
        return new PageInfo<>(ordersList,5);

    }

    @Override
    public List<Orders> runnerFindAll(Runner runner) {
        return ordersMapper.findAllByRunnerID(runner);
    }

    @Override
    public PageInfo<Orders> userWaitOrderFind(Integer id, Integer pageNo) {
        PageHelper.startPage(pageNo,5);
        List<Orders> ordersList =ordersMapper.findAllByUserIDandStatus(id);
        return new PageInfo<>(ordersList,3);
    }

    @Override
    public PageInfo<Orders> runnerFindWaitOrder(Integer pageNo,Integer CityID) {
        PageHelper.startPage(pageNo,5);
        List<Orders> ordersList=ordersMapper.findAllByStatusAndCityID(CityID);
        for (Orders orders : ordersList) {
            orders.setTask(taskService.findByID(orders.getTask().getId()));
        }
        return new PageInfo<>(ordersList,3);
    }

    @Override
    public Orders findOrder(Integer ID) {
        Orders orders = ordersMapper.findByID(ID);
        Tasks tasks =taskService.findByID(orders.getTask().getId());
        tasks.setUseraddress(userAddressService.findAddress(tasks.getUseraddress().getId()));
        orders.setTask(tasks);
        orders.setUsers(usersService.findUser(orders.getUsers().getId()));
        if (orders.getRunner()!=null)
            orders.setRunner(runnerService.findById(orders.getRunner().getId()));
        return orders;
    }

    @Override
    public void accept(Orders orders) {
        ordersMapper.UpdateRunnerIDAndAcceptTime(orders);
    }

    @Override
    public PageInfo<Orders> userFindAll(Integer page,Integer id) {
       PageHelper.startPage(page,5);
       List<Orders> ordersList=ordersMapper.findAllByUserID(id);
        return new PageInfo<>(ordersList,3);
    }

    @Override
    public PageInfo<Orders> runnerFindOrdering(Integer page, Integer ID) {
        PageHelper.startPage(page,5);
        List<Orders> ordersList=ordersMapper.findByRunnerIDAndStatus(ID);
        return new PageInfo<>(ordersList,3);
    }

    @Override
    public Orders findBeforeDelivery(Integer id) {
        return  ordersMapper.findMoreByID(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Integer id, Integer status) {
        ordersMapper.UpdateStatusByID(id,status);
        if (status==3) {
            Orders orders = ordersMapper.findByID(id);
         Tasks tasks= orders.getTask();
         taskService.complete(tasks.getId());
            HistoryOrders historyOrders =new HistoryOrders();
            historyOrders.setUser(orders.getUsers());
            historyOrders.setStatus(false);
            historyOrders.setRunner(orders.getRunner());
            historyOrders.setTask(tasks);
            Date date =new Date();
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date =simpleDateFormat.parse(simpleDateFormat.format(date));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            historyOrders.setCompletedAt(date);
            historyOrders.setCreatedAt(orders.getCreatedAt());
            histoOrderService.insert(historyOrders);
        }
    }

    @Override
    public void cancelBeforeAccept(Integer id) {
        ordersMapper.deleteByID(id);
    }

    @Override
    public Integer findRunnerTaskingNumber(Integer id) {
        return ordersMapper.selectTaskingNumberByRunnerID(id);
    }

    @Override
    public Integer findAllOrder() {
        return ordersMapper.selectAllNumber();
    }

    @Override
    public Integer findCompleteOrder() {
        return ordersMapper.selectCompleteNumber();
    }

    @Override
    public PageInfo<Orders> findUserCompleteOrderList(Integer id,Integer pageNo) {
        PageHelper.startPage(pageNo,7);
        List<Orders> ordersList=ordersMapper.selectListByUserID(id);

        return new PageInfo<>(ordersList,3);
    }

    @Override
    public PageInfo<Orders> findUserTaskingOrderList(Integer id, Integer pageNo) {
        PageHelper.startPage(pageNo,5);
        List<Orders> ordersList =ordersMapper.selectTaskingListByUserID(id);
        for (Orders orders : ordersList) {
            orders.setTask(taskService.findByID(orders.getTask().getId()));
            orders.setUsers(usersService.findUser(orders.getUsers().getId()));
          orders.setRunner(runnerService.findById(orders.getRunner().getId()));
        }
        return new PageInfo<>(ordersList,3);
    }

    @Override
    public Orders findByTaskID(Integer id) {

    return ordersMapper.findByTaskID(id);
    }

    @Override
    public void deleteUser(Integer id) {
        ordersMapper.deleteUserByID(id);
        Orders orders =ordersMapper.findByID(id);
        histoOrderService.deleteUserByTaskID(orders.getTask().getId());
    }

    @Override
    public void deleteAdmin(Integer id) {
        ordersMapper.deleteAdminByID(id);
        Orders orders =ordersMapper.findByID(id);
        histoOrderService.deleteAdminByTaskID(orders.getTask().getId());
    }

    @Override
    public void deleteRunner(Integer id) {
        ordersMapper.deleteRunnerByID(id);
        Orders orders =ordersMapper.findByID(id);
        histoOrderService.deleteRunnerByID(orders.getTask().getId());
    }

    @Override
    public void deleteUserByTaskID(Integer id) {
        ordersMapper.deleteUserByTaskID(id);
    }

    @Override
    public void deleteAdminByTaskID(Integer id) {
        ordersMapper.deleteAdminByTaskID(id);
    }

    @Override
    public void deleteRunnerByTaskID(Integer id) {
        ordersMapper.deleteRunnerByTaskID(id);
    }

    @Override
    public PageInfo<Orders> findCompletedByRunnerID(Integer id, Integer page) {

        PageHelper.startPage(page,5);
        List<Orders> ordersList =ordersMapper.findAllByRunnerIDAndStatus(id);
        for (Orders orders : ordersList) {
            orders.setTask(taskService.findByID(orders.getTask().getId()));
        }
       return new PageInfo<>(ordersList,5);
    }


}
